// DlgUV.cpp : implementation file
//

#include "stdafx.h"
#include "gp_uv.h"
#include "DlgUV.h"
#include "ExclusionST2.h"

using namespace nsExclusionStatic2;

// DlgUV dialog

IMPLEMENT_DYNAMIC(DlgUV, CDialog)

DlgUV::DlgUV(CWnd* pParent /*=NULL*/)
	: CDialog(DlgUV::IDD, pParent)
{
	m_Bitmap_Front_Path=_T("");
	m_Bitmap_Back_Path=_T("");
	m_pDlgInclusion = NULL;
}

DlgUV::~DlgUV()
{
	if ( m_pDlgInclusion ) delete m_pDlgInclusion;
}

void DlgUV::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EF_UV_IMAGE_FRONT, m_control_EF_IMAGE_FRONT);
	DDX_Control(pDX, IDC_PIC_UV_IMAGE_FRONT, m_IDC_PIC_UV_IMAGE_FRONT);
	DDX_Control(pDX, IDC_PIC_UV_IMAGE_BACK, m_IDC_PIC_UV_IMAGE_BACK);
	DDX_Control(pDX, IDC_EF_UV_IMAGE_BACK, m_control_EF_IMAGE_BACK);
}


BEGIN_MESSAGE_MAP(DlgUV, CDialog)
	ON_BN_CLICKED(IDC_CB_UV_AREA_FRONT, &DlgUV::OnBnClickedCbUvAreaFront)
	ON_BN_CLICKED(IDC_CB_UV_AREA_BACK, &DlgUV::OnBnClickedCbUvAreaBack)
	ON_BN_CLICKED(IDC_CB_UV_IMAGE_FRONT, &DlgUV::OnBnClickedCbUvImageFront)
	ON_BN_CLICKED(IDC_CB_UV_IMAGE_BACK, &DlgUV::OnBnClickedCbUvImageBack)
	ON_BN_CLICKED(IDC_PB_UV_IMAGE_FRONT, &DlgUV::OnBnClickedPbUvImageFront)
	ON_BN_CLICKED(IDC_PB_UV_IMAGE_BACK, &DlgUV::OnBnClickedPbUvImageBack)
	ON_WM_PAINT()
	ON_STN_CLICKED(IDC_PIC_UV_AREA_FRONT, &DlgUV::OnStnClickedPicUvAreaFront)
END_MESSAGE_MAP()


// DlgUV message handlers

BOOL DlgUV::OnInitDialog()
{
	CDialog::OnInitDialog();

	Areas_[ 0 ][ esFront ].iCurrentExclusion =
		Areas_[ 0 ][ esBack ].iCurrentExclusion = -1;

	// Create Area Definition Dialog
	if ( m_pDlgInclusion == NULL )
	{
		m_pDlgInclusion = new CInclusionDlgOffset( true, this );
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	
}

void DlgUV::OnBnClickedCbUvAreaFront()
{
	if(IsDlgButtonChecked(IDC_CB_UV_AREA_FRONT) == 0)
	{
		GetDlgItem(IDC_PIC_UV_AREA_FRONT)->EnableWindow(FALSE);
	}
	else
	{
		GetDlgItem(IDC_PIC_UV_AREA_FRONT)->EnableWindow(TRUE);
	}
}

void DlgUV::OnBnClickedCbUvAreaBack()
{
	if(IsDlgButtonChecked(IDC_CB_UV_AREA_BACK) == 0)
	{
		GetDlgItem(IDC_PIC_UV_AREA_BACK)->EnableWindow(FALSE);
	}
	else
	{
		GetDlgItem(IDC_PIC_UV_AREA_BACK)->EnableWindow(TRUE);
	}
}

void DlgUV::OnBnClickedCbUvImageFront()
{
	if(IsDlgButtonChecked(IDC_CB_UV_IMAGE_FRONT) == 0)
	{
		GetDlgItem(IDC_EF_UV_IMAGE_FRONT)->EnableWindow(FALSE);
		GetDlgItem(IDC_PB_UV_IMAGE_FRONT)->EnableWindow(FALSE);
		GetDlgItem(IDC_PIC_UV_IMAGE_FRONT)->EnableWindow(FALSE);

		m_control_EF_IMAGE_FRONT.SetWindowText(_T(""));
		
	}
	else
	{
		GetDlgItem(IDC_EF_UV_IMAGE_FRONT)->EnableWindow(TRUE);
		GetDlgItem(IDC_PB_UV_IMAGE_FRONT)->EnableWindow(TRUE);
		GetDlgItem(IDC_PIC_UV_IMAGE_FRONT)->EnableWindow(TRUE);
	}
}

void DlgUV::OnBnClickedCbUvImageBack()
{
	if(IsDlgButtonChecked(IDC_CB_UV_IMAGE_BACK) == 0)
	{
		GetDlgItem(IDC_EF_UV_IMAGE_BACK)->EnableWindow(FALSE);
		GetDlgItem(IDC_PB_UV_IMAGE_BACK)->EnableWindow(FALSE);
		GetDlgItem(IDC_PIC_UV_IMAGE_BACK)->EnableWindow(FALSE);

		m_control_EF_IMAGE_BACK.SetWindowText(_T(""));
	}
	else
	{
		GetDlgItem(IDC_EF_UV_IMAGE_BACK)->EnableWindow(TRUE);
		GetDlgItem(IDC_PB_UV_IMAGE_BACK)->EnableWindow(TRUE);
		GetDlgItem(IDC_PIC_UV_IMAGE_BACK)->EnableWindow(TRUE);
	}
}

void DlgUV::OnBnClickedPbUvImageFront()
{
	// Bitmap from file Front Image button clicked
	CFileDialog cfd(true,_T("*.bmp"),NULL,OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,_T("Bitmaps (*.bmp) |*.bmp||"),this);

	if(cfd.DoModal()!=IDOK)     
	{
		return;
	}
	m_Bitmap_Front_Path = cfd.GetPathName(); 
	m_control_EF_IMAGE_FRONT.SetWindowText(cfd.GetFileName());

	Show_Picture(m_Bitmap_Front_Path, m_IDC_PIC_UV_IMAGE_FRONT);   
}

void DlgUV::OnBnClickedPbUvImageBack()
{
	// Bitmap from file Back Image button clicked
	CFileDialog cfd(true,_T("*.bmp"),NULL,OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,_T("Bitmaps (*.bmp) |*.bmp||"),this);

	if(cfd.DoModal()!=IDOK)     
	{
		return;
	}
	m_Bitmap_Back_Path = cfd.GetPathName(); 
	m_control_EF_IMAGE_BACK.SetWindowText(cfd.GetFileName());

	Show_Picture(m_Bitmap_Back_Path, m_IDC_PIC_UV_IMAGE_BACK);  
}

void DlgUV::Show_Picture(CString &image_path, CStatic &image_control)
{
	if(image_path=="")        
	{
		return;
	}

	CRect rect;
	image_control.GetClientRect(&rect);    

	CImage image;       
	image.Load(image_path);   
	CDC* pDC = image_control.GetWindowDC();    
	image.Draw( pDC -> m_hDC,rect);      
	ReleaseDC(pDC);
}

void DlgUV::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CDialog::OnPaint() for painting messages
	if(IsDlgButtonChecked(IDC_CB_UV_IMAGE_FRONT) == 1)
		Show_Picture(m_Bitmap_Front_Path, m_IDC_PIC_UV_IMAGE_FRONT);

	if (IsDlgButtonChecked(IDC_CB_UV_IMAGE_BACK) == 1)
		Show_Picture(m_Bitmap_Back_Path, m_IDC_PIC_UV_IMAGE_BACK);
}

void DlgUV::OnStnClickedPicUvAreaFront()
{
	SEXCLUSION sex;
	sex.bActive = false;
	sex.Area.Rect.SetRect(200, 400, 25, 30);
	sex.Area.nOffsetX = 20;
	sex.Area.nOffsetY = 30;

	Areas_[0][0].iCurrentExclusion = -1;
	Areas_[0][0].vExclusionList.push_back(sex);

	m_pDlgInclusion->m_picExclusion.SetExclusionAreasFrom(&Areas_[0][0]);
	// Area Definition Front picture control clicked
	if ( m_pDlgInclusion->DoModal() == IDOK )
	{
	}
}


